package org.jeecg.glr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrVideo;
import org.jeecg.glr.entity.GlrVideoYs;
import org.jeecg.glr.mapper.GlrVideoMapper;
import org.jeecg.glr.service.IGlrLightPoleDeviceService;
import org.jeecg.glr.service.IGlrVideoService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 海康摄像头
 * @Author: jeecg-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@Service
public class GlrVideoServiceImpl extends ServiceImpl<GlrVideoMapper, GlrVideo> implements IGlrVideoService {


    @Resource
    GlrVideoMapper glrVideoMapper;

    @Autowired
    private IGlrVideoYsService glrVideoYsService;

    @Autowired
    private IGlrLightPoleDeviceService glrLightPoleDeviceService;

    @Override
    public void saveEntity(GlrVideo glrVideo){
        glrVideoMapper.insert(glrVideo);
        glrLightPoleDeviceService.dataProecss(glrVideo,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_ADD);
    }
    @Override
    public void updateEntityById(GlrVideo glrVideo){
        glrLightPoleDeviceService.dataProecss(glrVideo,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_EDIT);
        glrVideoMapper.updateById(glrVideo);
    }
    @Override
    public void removeEntityById(String id){
        GlrVideo glrVideo=glrVideoMapper.selectById(id);
        glrVideoMapper.deleteById(id);
        glrLightPoleDeviceService.dataProecss(glrVideo,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
    }

    @Override
    public void removeEntityByIds(List<String> asList){
        for(String stId:asList){
            GlrVideo glrVideo=glrVideoMapper.selectById(stId);
            glrVideoMapper.deleteById(stId);
            glrLightPoleDeviceService.dataProecss(glrVideo,GLRConstant.GLR_LIGHT_POLE_DEVICE_TYPE_DELETE);
        }
    }



    @Override
    public List<String> getGlrVideoStatus() {
        String sql="select equipment_status from glr_video where 1=1";
       /* if(!SysUtil.getUserInfo().getUserType().equals(SysConstant.SYS_USER_TYPE_ADMIN)){
                sql=sql+" and sys_company_code='"+SysUtil.getUserInfo().getSysCompanyCode()+"'";
        }*/
        return glrVideoMapper.getStatus(sql);
    }

    @Override
    public void doUpdateStatus(){
        List<GlrVideo> videoList = glrVideoMapper.selectList(new QueryWrapper<GlrVideo>());
        //GlrVideoYs glrVideoYs=glrVideoYsService.getById(GLRConstant.GLR_VIDEO_YS_GAOTAI_ID);

        List<String> stListY=new ArrayList<>();
        List<String> stListN=new ArrayList<>();
        for(GlrVideo domain : videoList){
            if (StringUtil.isNotEmpty(domain.getVideoYsCode())){
                GlrVideoYs glrVideoYs=null;
                if(StringUtil.isNotEmpty(domain.getVideoYsCode())){
                    QueryWrapper queryWrapperFind = QueryGenerator.initQueryWrapperCode();
                    queryWrapperFind.eq("code",domain.getVideoYsCode());
                    List<GlrVideoYs> listFind = glrVideoYsService.list(queryWrapperFind);
                    if (listFind.size()>0){
                        glrVideoYs=listFind.get(0);
                    }
                }

                if (glrVideoYs!=null){
                    domain.setGlrVideoYs(glrVideoYs);
                    String stToken = glrVideoYsService.getToken(glrVideoYs);
                    String stEquipmentStatus = glrVideoYsService.getStatus(domain, stToken);
                    domain.setEquipmentStatus(stEquipmentStatus);
                }else {
                    domain.setEquipmentStatus(GLRConstant.YN_N);
                }
            }else {
                domain.setEquipmentStatus(GLRConstant.YN_N);
            }
            if (GLRConstant.YN_Y.equals(domain.getEquipmentStatus())){
                stListY.add(domain.getId());
            }
            if (GLRConstant.YN_N.equals(domain.getEquipmentStatus())){
                stListN.add(domain.getId());
            }
            //glrVideoMapper.updateById(domain);
        }
        if (stListY.size()>0){
            String stSql= org.jeecg.glr.util.StringUtil.joinString1(stListY,",");
            stSql="update glr_video set equipment_status='Y'  where equipment_status!='Y' and id in ("+stSql+")";
            glrVideoYsService.updateSql(stSql);
        }

        if (stListN.size()>0){
            String stSql= org.jeecg.glr.util.StringUtil.joinString1(stListN,",");
            stSql="update glr_video set equipment_status='N'  where equipment_status!='N' and id in ("+stSql+")";
            glrVideoYsService.updateSql(stSql);
        }



        //List<String> sqlList=new ArrayList<>();
        //sqlList.add("update gl_video set status='ttt' where id='729f9d7e6cd711ea977e02004c4f4f50';");
        //sqlList.add("update gl_video set status='ttt' where id='729fa0b66cd711ea977e02004c4f4f50';");
        //sqlList.add("update gl_video set status='ttt' where id='729fb1526cd711ea977e02004c4f4f50';");
        //glrVideoYsService.updateBatch(sqlList);
    }


}
